package com.yingxin.yx.framework.web.resp;

import com.yingxin.yx.framework.commons.GlobalCode;
import com.yingxin.yx.framework.commons.GlobalInstance;
import com.yingxin.yx.framework.commons.StatusCode;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @author fanchao
 */
@Data
@ToString
public class RespHttp<T> implements Serializable {

    private static final long serialVersionUID = -8249046841631351818L;

    private Boolean success;

    private Long ts = System.currentTimeMillis();


    /**
     * 状态码
     */
    private String code;

    /**
     * 消息
     */
    private String msg;

    /**
     *
     */
    private String tokenExpire;

    /**
     * 返回数据
     */
    private T data;


    public static RespHttp ok() {
        RespHttp resp = new RespHttp();
        resp.setSuccess(true);
        resp.setCode(GlobalCode.OK);
        resp.setMsg("");
        return resp;
    }

    public static <T> RespHttp<T> ok(T data) {
        RespHttp<T> resp = new RespHttp<>();
        resp.setSuccess(true);
        resp.setCode(GlobalCode.OK);
        resp.setMsg("");
        resp.setData(data);
        return resp;
    }

    /*
    public static RespHttp error() {
        RespHttp resp = new RespHttp();
        resp.setCode(RespCode.ERROR);
        resp.setMessage("");
        return resp;
    }
    */


    public static RespHttp loginFail(String msg) {
        RespHttp resp = new RespHttp();
        resp.setSuccess(false);
        resp.setCode("401");
        resp.setMsg(msg);
        return resp;
    }

    public static RespHttp error(String code) {
        StatusCode statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(code);
        RespHttp resp = new RespHttp();
        resp.setSuccess(false);
        if (statusCode == null) {
            // statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(BaseConst.BaseCode.BIZ_EXCEPTION);
            resp.setCode(GlobalCode.BIZ_EXCEPTION);
            resp.setMsg(code);
            return resp;
        } else {
            resp.setCode(statusCode.getCode());
            resp.setMsg(statusCode.getMessage());
            return resp;
        }

        //resp.setCode(code);
        //resp.setMessage("");
        //return resp;
    }

    public static RespHttp error(String code, String message) {
        StatusCode statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(code);
        RespHttp resp = new RespHttp();
        resp.setSuccess(false);
        if (statusCode == null) {
            // statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(BaseConst.BaseCode.BIZ_EXCEPTION);
            resp.setCode(GlobalCode.BIZ_EXCEPTION);
            resp.setMsg(message);
            return resp;
        } else {
            resp.setCode(code);
            resp.setMsg(StringUtils.isEmpty(message) ? statusCode.getMessage() : statusCode.getMessage() + ":" + message);
            return resp;
        }

        //resp.setCode(code);
        //resp.setMessage(message);
        //return resp;
    }

}